package kr.hkit.androidcomplete03.ch28;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;

import kr.hkit.androidcomplete03.R;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class WebServiceGet extends Activity implements OnClickListener {
	TextView tv;
	ProgressDialog progressDlg;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webservice);
		findViewById(R.id.reqbtn).setOnClickListener(this);
		tv = (TextView) findViewById(R.id.result);
	}

	public void onClick(View v) {
		String str = "http://www.kma.go.kr/wid/queryDFSRSS.jsp?zone=2726067000";
		new HttpRequestTask().execute(str);
	}

	private class HttpRequestTask extends AsyncTask<String, Void, String> {
		protected void onPreExecute() {
			progressDlg = ProgressDialog.show(WebServiceGet.this, "Wait",
					"Downloading...");
		}

		protected String doInBackground(String... strs) {
			StringBuffer sb = new StringBuffer();
			try {
				HttpClient client = new DefaultHttpClient();
				HttpGet get = new HttpGet(strs[0]);
				HttpResponse responseGet = client.execute(get);
				HttpEntity resEntity = responseGet.getEntity();
				if (resEntity != null) {
					sb.append(EntityUtils.toString(resEntity));
				}
			} catch (MalformedURLException e) {
			} catch (UnsupportedEncodingException e) {
			} catch (ClientProtocolException e) {
			} catch (IOException e) {
			}
			return sb.toString();
		}

		protected void onPostExecute(String result) {
			progressDlg.dismiss();
			tv.setText(result);
		}
	}// end of class HttpRequestTask
}// end of class WebServiceGet